﻿
using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using AppSettings;
using KEngine;
using KEngine.UI;
using KSFramework;

public class Game : KSGame
{
	/// <summary>
	/// Add Your Custom Initable(Coroutine) Modules Here...
	/// </summary>
	/// <returns></returns>
	protected override IList<IModuleInitable> CreateModules()
	{
		var modules = base.CreateModules();

		// TIP: Add Your Custom Module here
		//modules.Add(new Module());

		return modules;
	}

	/// <summary>
	/// Before Init Modules, coroutine
	/// </summary>
	/// <returns></returns>
	public override IEnumerator OnBeforeInit()
	{
		// Do Nothing
		yield break;
	}

	/// <summary>
	/// After Init Modules, coroutine
	/// </summary>
	/// <returns></returns>
	public override IEnumerator OnGameStart()
	{

		// Print AppConfigs
		Log.Info("======================================= Read Settings from C# =================================");
		foreach (GameConfigSetting setting in GameConfigSettings.GetAll())
		{
			Debug.Log(string.Format("C# Read Setting, Key: {0}, Value: {1}", setting.Id, setting.Value));
		}

		yield return null;

		Log.Info("======================================= Open Window 'MenuPanel' =================================");
		//UIModule.Instance.OpenWindow("Login", 888);
		UIModule.Instance.OpenWindow("MenuPanel");
		// Test Load a scene in asset bundle
		//SceneLoader.Load("Scene/TestScene/TestScene.unity");
		//SceneLoader.Load("Scene/DialScene");

		// 开始加载我们的公告界面！
		//UIModule.Instance.OpenWindow("Billboard");

		//var path = "ui/enteranceicon.prefab";
		//var assetLoader = InstanceAssetLoader.Load(path);
		//while (!assetLoader.IsCompleted)
		//	yield return null;

		//var obj = assetLoader.ResultObject as GameObject;

		//UIModule.Instance.OpenWindow();

		//StartCoroutine(InstanceAssetLoader.LoadObj("ui/enteranceicon.prefab", (obj) =>
		//{
		//	//KSFramework.LuaBehaviour.Create(obj, "Behaviour/TestLuaBehaviour");
		//	//var controller = obj.AddComponent<LuaUIController>();
		//	//controller.UITemplateName = "Behaviour";
		//	//controller.UIName = "TestLuaBehaviour";
		//	//controller.OnInit();
		//	Debug.LogError("我是Lua加载的");
		//}));
		

		//assetLoader.Release();


		// 测试Collect函数，立即回收所有资源
		//var path = "ui/billboard.prefab";
		//var assetLoader = InstanceAssetLoader.Load(path); 
		//while (!assetLoader.IsCompleted)             
		//	yield return null;         
		//var assetLoader2 = InstanceAssetLoader.Load(path);       
		//while (!assetLoader2.IsCompleted)             
		//	yield return null;         
		//assetLoader2.Release();
		//assetLoader.Release();

		KResourceModule.Collect();
	}

	void Load()
	{

	}

}
